import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeSupport;

import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

/**
 * Prompts the player with information fields so that he/she can give the ship's
 * crew they're name, profession, and starting rations and speed. After this the
 * game loads into the Shop menu.
 */
public class TitleScreen extends State
{
    private JFrame frame;
    private JTextField PlayerNameField;
    private JTextField CrewField1;
    private JTextField CrewField2;
    private JTextField CrewField3;
    private JTextField CrewField4;
    private JRadioButton FishermanBtn, CarpenterBtn, DoctorBtn, k40, k20, k10,
            wellFed, modest, meager;

    /**
     * Create the application. initializes State ship
     * 
     * @param ship
     *            object "ship"
     */
    public TitleScreen(Ship ship)
    {
        super();
        theShip = ship;
        initialize();
    }

    @Override
    public void paintComponent(Graphics g)
    {
        Image img = new ImageIcon("paper1.jpg").getImage();
        g.drawImage(img, 0, 0, null);
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize()
    {
        this.setLayout(new BorderLayout());

        JPanel WelcomePanel = new JPanel();
        this.add(WelcomePanel, BorderLayout.NORTH);
        WelcomePanel.setOpaque(false); // transparency

        JLabel welcomeLabel = new JLabel("Welcome to the Age of Discovery!");
        welcomeLabel.setFont(new Font("Papyrus", Font.ITALIC, 20));
        welcomeLabel.setForeground(Color.WHITE);

        JPanel profPanel = new JPanel();
        this.add(profPanel, BorderLayout.SOUTH);
        profPanel.setOpaque(false); // transparency

        JLabel lblWhichAreYou = new JLabel("Which are you? :");
        lblWhichAreYou.setForeground(Color.white);
        profPanel.add(lblWhichAreYou);

        FishermanBtn = new JRadioButton("Fisherman");
        FishermanBtn.setOpaque(false);
        FishermanBtn.setForeground(Color.WHITE);
        FishermanBtn
                .setToolTipText("Fishermen make the most of their food, and can catch it as they go, saving money!");
        profPanel.add(FishermanBtn);

        CarpenterBtn = new JRadioButton("Carpenter");
        CarpenterBtn.setOpaque(false);
        CarpenterBtn.setForeground(Color.white);
        CarpenterBtn
                .setToolTipText("Carpenters are very resourceful, and won't require as many parts for ship repairs, giving you peace of mind during storms!");
        profPanel.add(CarpenterBtn);

        DoctorBtn = new JRadioButton("Doctor");
        DoctorBtn.setOpaque(false);
        DoctorBtn.setForeground(Color.white);
        DoctorBtn
                .setToolTipText("Doctors make the most of their medicine, keeping their crew and themselves healthy. They also start with the most money!");
        profPanel.add(DoctorBtn);

        JPanel ContainerPanel = new JPanel();
        this.add(ContainerPanel, BorderLayout.CENTER);
        ContainerPanel.setLayout(new GridLayout(0, 2, 0, 0));
        ContainerPanel.setOpaque(false); // transparency

        JPanel CaptainPanel = new JPanel();
        ContainerPanel.add(CaptainPanel);
        CaptainPanel.setOpaque(false); // transparency

        JLabel lblWhatsYourName = new JLabel("What's your name? : ");
        lblWhatsYourName.setForeground(Color.white);
        CaptainPanel.add(lblWhatsYourName);

        PlayerNameField = new JTextField();
        PlayerNameField.setText("<Enter Name Here>");
        CaptainPanel.add(PlayerNameField);
        PlayerNameField.setColumns(15);

        JLabel lblChooseAStarting = new JLabel("Choose a starting pace :");
        lblChooseAStarting.setForeground(Color.white);
        CaptainPanel.add(lblChooseAStarting);

        k40 = new JRadioButton("40 Knots");
        k40.setFont(new Font("Lucida Grande", Font.PLAIN, 10));
        k40.setOpaque(false);
        k40.setForeground(Color.white);
        CaptainPanel.add(k40);

        k20 = new JRadioButton("20 Knots");
        k20.setFont(new Font("Lucida Grande", Font.PLAIN, 10));
        k20.setOpaque(false);
        k20.setForeground(Color.white);
        CaptainPanel.add(k20);

        k10 = new JRadioButton("10 Knots");
        k10.setFont(new Font("Lucida Grande", Font.PLAIN, 10));
        k10.setOpaque(false);
        k10.setForeground(Color.white);
        CaptainPanel.add(k10);

        JLabel lblChooseYourStarting = new JLabel(
                "Choose your starting rations :");
        lblChooseYourStarting.setForeground(Color.white);
        CaptainPanel.add(lblChooseYourStarting);

        wellFed = new JRadioButton("Well Fed");
        wellFed.setFont(new Font("Lucida Grande", Font.PLAIN, 10));
        wellFed.setOpaque(false);
        wellFed.setForeground(Color.white);
        CaptainPanel.add(wellFed);

        modest = new JRadioButton("Modest");
        modest.setFont(new Font("Lucida Grande", Font.PLAIN, 10));
        modest.setOpaque(false);
        modest.setForeground(Color.white);
        CaptainPanel.add(modest);

        meager = new JRadioButton("Meager");
        meager.setFont(new Font("Lucida Grande", Font.PLAIN, 10));
        meager.setOpaque(false);
        meager.setForeground(Color.white);
        CaptainPanel.add(meager);

        JPanel CrewPanel = new JPanel();
        ContainerPanel.add(CrewPanel);
        CrewPanel.setOpaque(false);

        JLabel lblWhoAreYourCrew = new JLabel("Who are your crew mates? :");
        lblWhoAreYourCrew.setForeground(Color.white);
        CrewPanel.add(lblWhoAreYourCrew);

        CrewField1 = new JTextField();
        CrewField1.setText("<Enter Name Here>");
        CrewPanel.add(CrewField1);
        CrewField1.setColumns(15);

        CrewField2 = new JTextField();
        CrewField2.setText("<Enter Name Here>");
        CrewPanel.add(CrewField2);
        CrewField2.setColumns(15);

        CrewField3 = new JTextField();
        CrewField3.setText("<Enter Name Here>");
        CrewPanel.add(CrewField3);
        CrewField3.setColumns(15);

        CrewField4 = new JTextField();
        CrewField4.setText("<Enter Name Here>");
        CrewPanel.add(CrewField4);
        CrewField4.setColumns(15);

        JButton goButton = new JButton("Set Sail!");
        goButton.addActionListener(new GoListener());
        // profPanel.add(goButton, BorderLayout.SOUTH);
        profPanel.add(goButton);

        ButtonGroup group = new ButtonGroup();
        group.add(FishermanBtn);
        group.add(CarpenterBtn);
        group.add(DoctorBtn);
        ButtonGroup group1 = new ButtonGroup();
        group1.add(k40);
        group1.add(k20);
        group1.add(k10);
        ButtonGroup group2 = new ButtonGroup();
        group2.add(wellFed);
        group2.add(modest);
        group2.add(meager);
    }

    public String getType(){
    	return "TitleScreen";
    }
    
    /**
     * ActionListener for the "Set Sail" button in the introductory screen. Sets
     * everything to proper amounts and names, and opens up the game.
     */
    private class GoListener implements ActionListener
    {
        @Override
        public void actionPerformed(ActionEvent e)
        {
            boolean validShip = true;

            // PROFESSION CHECK
            if (FishermanBtn.isSelected()) theShip.setProf("Fisher");
            else if (CarpenterBtn.isSelected()) theShip.setProf("Carpenter");
            else if (DoctorBtn.isSelected()) theShip.setProf("Doctor");
            else
            {
                validShip = false;
                JOptionPane.showMessageDialog(frame, "No Profession Selected!",
                        "No Profession!", JOptionPane.WARNING_MESSAGE);
            }

            // PACE CHECK
            if (k40.isSelected()) theShip.setPace(30);
            else if (k20.isSelected()) theShip.setPace(20);
            else if (k10.isSelected()) theShip.setPace(10);
            else
            {
                validShip = false;
                JOptionPane.showMessageDialog(frame, "Pace Not Selected!",
                        "No Pace!", JOptionPane.WARNING_MESSAGE);
            }

            // RATION CHECK
            if (wellFed.isSelected()) theShip.setRations(3);
            else if (modest.isSelected()) theShip.setRations(2);
            else if (meager.isSelected()) theShip.setRations(1);
            else
            {
                validShip = false;
                JOptionPane.showMessageDialog(frame, "Rations Not Selected!",
                        "No Rations!", JOptionPane.WARNING_MESSAGE);
            }

            // CAPTAIN NAME CHECK
            String captainName = PlayerNameField.getText();
            if (captainName.equals("")
                    || captainName.equals("<Enter Name Here>"))
            {
                validShip = false;
                JOptionPane.showMessageDialog(frame,
                        "Player Name Not Entered!",
                        "A Ship Without A Captain!",
                        JOptionPane.WARNING_MESSAGE);
            }
            else theShip.setCaptainName(captainName);

            // CREW NAMES CHECK
            String crewName1 = CrewField1.getText();
            String crewName2 = CrewField2.getText();
            String crewName3 = CrewField3.getText();
            String crewName4 = CrewField4.getText();

            if (crewName1.equals("") || crewName2.equals("")
                    || crewName3.equals("") || crewName4.equals("")
                    || crewName1.equals("<Enter Name Here>")
                    || crewName2.equals("<Enter Name Here>")
                    || crewName3.equals("<Enter Name Here>")
                    || crewName4.equals("<Enter Name Here>"))
            {
                validShip = false;
                JOptionPane.showMessageDialog(frame, "Crew Names Not Entered!",
                        "No Crew!", JOptionPane.WARNING_MESSAGE);
            }
            else
            {
                String[] names = { crewName1, crewName2, crewName3, crewName4 };
                theShip.setCrewNames(names);
            }

            if (validShip)
            {
                status.firePropertyChange("finished", false, true);
            }
        }// end actionPerformed method
    }// end private listener class

}// end TitleScreen class